﻿using System;
using System.Web;
using System.Xml.Linq;

namespace KMF.Utility
{
    public class GoogleMapsApiUtil
    {
        public static XElement GetGeocodingSearchResults(string address)
        {

            // Use the Google Geocoding service to get information about the user-entered address
            // See http://code.google.com/apis/maps/documentation/geocoding/index.html for more info...
            var url = string.Format("http://maps.google.com/maps/api/geocode/xml?address={0}&sensor=false",
                                    HttpContext.Current.Server.UrlEncode(address));

            // Load the XML into an XElement object (whee, LINQ to XML!)
            XElement results = null;
            try
            {
                results = XElement.Load(url);
                var status = results.Element("status").Value;
                if (status != "OK" && status != "ZERO_RESULTS")
                {
                    results = null;
                }
                if (status == "ZERO_RESULTS" || status == "OVER_QUERY_LIMIT" || status == "REQUEST_DENIED" || status == "INVALID_REQUEST")
                {
                    results = null;
                }
            }
            catch(Exception ex)
            {
                
            }
            // Check the status
            
            return results;
        }
    }
}
